package hu.plusone.fakecommander.flickr;

import hu.plusone.fakecommander.Storage;
import hu.plusone.fakecommander.util.Coupler;
import hu.plusone.fakecommander.util.Packer;
import hu.plusone.fakecommander.util.Utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FlickrStorage implements Storage {

    private static final Logger LOGGER = LoggerFactory.getLogger(FlickrStorage.class);
    private final Packer packer;
    private final Coupler<byte[]> coupler;
    private final FlickrApi flickr;

    public FlickrStorage(Packer packer, Coupler<byte[]> coupler, FlickrApi flickr) {
        this.packer = packer;
        this.coupler = coupler;
        this.flickr = flickr;
    }

    public void createFolder(String folderPath) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<String> listFilesInFolder(String folderPath) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<String> listFoldersInFolder(String folderPath) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void remove(String path) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void put(String filePath, InputStream is) {
        int lastIdx = filePath.lastIndexOf("/");
        if (lastIdx == -1) {
            put("/", filePath, is);
            return;
        }
        String folderPath = filePath.substring(0, lastIdx);
        String filename = filePath.substring(lastIdx + 1);
        put(folderPath, filename, is);
    }

    public void put(String folderPath, String filename, InputStream is) {
        folderPath = Utils.encodeBase64(folderPath);
        filename = Utils.encodeBase64(filename);
        flickr.update(folderPath, filename, pack(is));
    }

    public InputStream get(String folderPath, String filename) {
        folderPath = Utils.encodeBase64(folderPath);
        filename = Utils.encodeBase64(filename);
        InputStream is = flickr.read(folderPath, filename);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        packer.unPack(is, os);
        return new ByteArrayInputStream(os.toByteArray());
    }

    public InputStream get(String filePath) {
        int lastIdx = filePath.lastIndexOf("/");
        if (lastIdx == -1) {
            return get("/", filePath);
        }
        String folderPath = filePath.substring(0, lastIdx);
        String filename = filePath.substring(lastIdx + 1);
        return get(folderPath, filename);
    }

    private byte[] pack(InputStream is) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        packer.pack(is, os);
        return os.toByteArray();
    }
}
